﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using SilverlightApp.data;
using SilverlightApp.Common;

namespace SilverlightApp.Web
{
    public class LinearChartBuilder
    {
        private Chart chart = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="panel"></param>
        /// <param name="dsList">list长度为3，第一位是温度，第二位是湿度，第三位是光照</param>
        public void BuildChart(Panel panel, List<MyObservableCollection<PairInfo>> dsList)
        {
            chart = new Chart { Title = "历史监测" };
            DateTimeAxis dateAxis = new DateTimeAxis { Orientation = AxisOrientation.X, };
            dateAxis.Minimum = DateTime.MinValue;
            dateAxis.Maximum = dateAxis.Minimum.Value.AddMinutes(5);
            chart.Axes.Add(dateAxis);
            LinearAxis valueAxis = new LinearAxis { Orientation = AxisOrientation.Y, Minimum = 0, Maximum = 150, ShowGridLines = true };
            valueAxis.Interval = 30;
            chart.Width = 1050;
            chart.Height = 250;
            chart.Axes.Add(valueAxis);
            int addSecond = 2;
            bool firstChange = true;
            for (int i = 0; i < 3; i++)
            {
                string titleStr = i == 0 ? "温度:{0} °C" : i == 1 ? "湿度:{0} %" : "光照:{0} lx";
                Color c = i == 0 ? Colors.Red : i == 1 ? Colors.Blue : Color.FromArgb(255, 0, 255, 0);
                MyObservableCollection<PairInfo> ds = dsList[i];
                DataPointSeries series = GetSeries(ds, c);
                series.Title = string.Format(titleStr, 0);
                ds.AfterPropertyChanged = (self) =>
                {
                    if (self != null && self.Count > 0)
                    {
                        var lastValue = self[self.Count - 1];
                        double vualue = Math.Round(lastValue.Value, 2);
                        series.Title = string.Format(titleStr, vualue);
                        if (firstChange)
                        {
                            dateAxis.Maximum = lastValue.Time.AddMinutes(5);
                            dateAxis.Minimum = lastValue.Time;
                            firstChange = false;
                        }
                        else if (dateAxis.Maximum.Value < lastValue.Time)
                        {
                            dateAxis.Maximum = dateAxis.Maximum.Value.AddSeconds(addSecond);
                            dateAxis.Minimum = dateAxis.Minimum.Value.AddSeconds(addSecond);
                        }

                        //valueAxis.Interval = (int)(((valueAxis.Maximum.Value - valueAxis.Minimum.Value) / 6 + 10) / 10) * 10;
                        if (valueAxis.Maximum.Value < vualue)
                        {
                            valueAxis.Maximum = vualue + 10;
                        }
                    }
                };
                chart.Series.Add(series);
            }
            Grid grid = new Grid();
            grid.Children.Add(chart);
            panel.Children.Add(grid);
        }

        private DataPointSeries GetSeries(IEnumerable<PairInfo> ds, Color c)
        {
            DataPointSeries series = new LineSeries();
            series.ItemsSource = ds;
            series.IndependentValueBinding = new System.Windows.Data.Binding("Time");
            series.DependentValueBinding = new System.Windows.Data.Binding("Value");
            Style lineStyle2 = new System.Windows.Style();
            Setter lineTemplate = new Setter(System.Windows.Controls.Control.TemplateProperty, null);
            Setter setterBlue = new Setter(System.Windows.Controls.Control.BackgroundProperty, new SolidColorBrush(c));
            //lineStyle2.Setters.Add(lineTemplate);
            lineStyle2.Setters.Add(setterBlue);
            lineStyle2.TargetType = typeof(System.Windows.Controls.Control);
            series.DataPointStyle = lineStyle2;

            return series;
        }
    }
}
